# 第十七次课作业，简单的注册、登录功能

import pymysql
db = pymysql.connect(host='49.233.39.160',
                     user='user',
                     password='leboAa!#$123',
                     port=3306,
                     charset='utf8',
                     db='lebo16')
cs = db.cursor()

while True:
    num = input('请输入你要进行的操作，1代表注册，2代表登录，输入其他字符则退出程序：')
    if num == '1':
        name = input('请输入账号：')
        name_password = input('请输入密码：')
        sex = input('请输入性别：')
        age = input('请输入年龄：')
        sql = """
        select * from myzstudents where name=%s and name_password=%s;
        """
        count_num = cs.execute(sql,[name,name_password])
        db.commit()

        if count_num == 1:
            print('账号已存在，请直接登录！')
        else:
            sql1 = """
            insert into myzstudents(name,name_password,sex,age) values(%s,%s,%s,%s);
            """
            cs.execute(sql1,[name,name_password,sex,age])
            db.commit()
            print('注册成功！')

    elif num == '2':
        name1 = input('请输入账号：')
        name_password1 = input('请输入密码：')
        sql2 = """
        select * from myzstudents where name=%s and name_password=%s
        """
        cs.execute(sql2,[name1,name_password1])
        print('登录成功！')

    else:
        print('你输入的数字非1或2，现在退出程序！')
        cs.close()
        db.close()
        break

